
var layer = layui.layer, //弹层
    table = layui.table, //表格
    form = layui.form,
    $ = layui.jquery,
    laydate = layui.laydate,
    currencyName = decodeURI(getQueryVariable("currencyName"))//中文解码,
;
lay('.starttime').each(function(){
    laydate.render({
        elem: this
    });
});


layer.ready(function () {
    $.ajax({
        url:"otherOutStock/getAllUserInfo",
        success:function(data){
            if(data.status == 200){
                for(let i in data.data){
                    $('#salesmanCode').append(new Option(data.data[i].name, data.data[i].code));
                }
                $('#salesmanCode').val(user.code);
                form.render('select');
            }
        }
    });
});
let importExchangeData = [];
table.render({
    elem: '#mains'
    ,data:importExchangeData
    ,cols: [[
        {checkbox: true, sort : true,title:'选择'}
        ,{field:'invoiceNumber', title:'发票编号',sort:true}
        ,{field:'contractNo', title:'合同编号',sort:true}
        ,{field:'claimCustomer', title:'客户名称',sort:true}
        ,{field:'claimSalesman', title:'业务员',sort:true}
        ,{field:'operator', title:'操作员',sort:true}
        ,{field:'totalInvoice', title:'发票总金额',sort:true}
        ,{field:'totalContract', title:'合同总金额',sort:true}
        ,{field:'contractAmountReceived', title:'合同已收金额',sort:true}
        ,{field:'outstandingAmountContract', title:'合同未收款金额',sort:true}
        ,{field:'settlementAmount', title:'结汇合同金额',sort:true}
        ,{field:'amountSplit', title:'已拆分金额',sort:true}
        ,{field:'collectionAmount', title:'结汇合同未收金额',sort:true}
    ]]
    ,id: 'mainstableid'
    ,height: 400
    , parseData: function(res){
        return {
            "code": res.status,
            "count": res.total,
            "data": res.data
        };
    }
});

form.on('submit(submitform)', function(data){
    data.field.currencyName=currencyName
    table.reload('mainstableid', {
        where: data.field,
        url:"receivePayment/getImportExchangeInfoByNotPage"
    }, 'data');
    return false;
});

$("#choose").on("click",function () {
    //第一个表格选中的数据
    let productData =   table.checkStatus("mainstableid").data;
    if(productData.length == 0){
        layer.msg("请选择数据")
    }else{
        //获取父页面明细数据
        let oldData =  parent.table.cache["claimdetailtableid"];
        let num = oldData.length+1;
        for(let i in productData){
            productData[i].tempId = GenNonDuplicateID(3) + num;
            productData[i].foreignBankDeductions = 0;
            productData[i].allocatedAmount = 0;
            productData[i].refundAmount = 0;
            //已确认默认为0
            productData[i].confirmed = 0;

            oldData.push(productData[i]);
            num++;
        }
        parent.table.reload('claimdetailtableid',{data : oldData, limit: oldData.length});
        //执行接口
        layer.msg("保存成功", {icon: 6});
        setTimeout(function () {
            //保存成功则关闭此页面
            let index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        },)
    }
})
$("#shutdown").on("click",function () {
    let index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
})

//生成临时id
function GenNonDuplicateID(randomLength){
    return Number(Math.random().toString().substr(3,randomLength) + Date.now()).toString(36)
}